package com.aistar.controller;

import com.aistar.pojo.Customer;
import com.aistar.service.CustomerService;
import com.aistar.service.MessageService;
import com.aistar.service.QuestionSetService;
import com.aistar.serviceVO.*;
import com.aistar.util.MessageUtil;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/center/{id}")
public class PersonCenterController {
    private final CustomerService customerService;
    private final DynamicAllVOService dynamicAllVOService;
    private final DynamicCommentVOService dynamicCommentVOService;
    private final DynamicAndPraiseVOService dynamicAndPraiseVOService;
    private final DynamicAndCollectVOService dynamicAndCollectVOService;
    private final ContestAndMessageVOService contestAndMessageVOService;
    private final MessageService messageService;
    private final ProgramRecordVOService programRecordVOService;
    private final QuestionSetService questionSetService;
    private final ChoiceAndCollectVOService choiceAndCollectVOService;
    private final ProgramAndCollectVOService programAndCollectVOService;

    @Autowired
    public PersonCenterController(
            CustomerService customerService,
            DynamicAllVOService dynamicAllVOService,
            DynamicCommentVOService dynamicCommentVOService,
            DynamicAndPraiseVOService dynamicAndPraiseVOService,
            DynamicAndCollectVOService dynamicAndCollectVOService,
            ContestAndMessageVOService contestAndMessageVOService,
            MessageService messageService,
            ProgramRecordVOService programRecordVOService,
            QuestionSetService questionSetService,
            ChoiceAndCollectVOService choiceAndCollectVOService,
            ProgramAndCollectVOService programAndCollectVOService) {
        this.customerService = customerService;
        this.dynamicAllVOService = dynamicAllVOService;
        this.dynamicCommentVOService = dynamicCommentVOService;
        this.dynamicAndPraiseVOService = dynamicAndPraiseVOService;
        this.dynamicAndCollectVOService = dynamicAndCollectVOService;
        this.contestAndMessageVOService = contestAndMessageVOService;
        this.messageService = messageService;
        this.programRecordVOService = programRecordVOService;
        this.questionSetService = questionSetService;
        this.choiceAndCollectVOService = choiceAndCollectVOService;
        this.programAndCollectVOService = programAndCollectVOService;
    }

    /* ================================== 基本资料模块 ================================== */
    @RequestMapping("/basic")
    public ModelAndView basic(HttpServletRequest request, @PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("customer/personCenter");
        return this.setCommonPart(mav, id, "basic", 1);
    }

    // 编辑基本资料
    @RequestMapping("/basic/edit")
    public ModelAndView editBasic(HttpServletRequest request, @PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("customer/personCenter");
        return this.setCommonPart(mav, id, "basic", 2);
    }

    /* ==================================== 动态模块 ==================================== */
    @RequestMapping("/dynamic")
    public ModelAndView dynamic(HttpServletRequest request, @PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("customer/personCenter");
        ServerResponse serverResponse = dynamicAllVOService.getByCustomerId(id);
        if (!MessageUtil.STATUS_FAIL.equals(serverResponse.getStatus()))
            mav.addObject("dynamicAllVOList", serverResponse.getData());
        return this.setCommonPart(mav, id, "dynamic", 1);
    }

    @RequestMapping("/dynamic/comment")
    public ModelAndView dynamicComment(HttpServletRequest request, @PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("customer/personCenter");
        ServerResponse serverResponse = dynamicCommentVOService.getByCustomerId(id);
        if (!MessageUtil.STATUS_FAIL.equals(serverResponse.getStatus()))
            mav.addObject("dynamicCommentVOList", serverResponse.getData());
        return this.setCommonPart(mav, id, "dynamic", 2);
    }

    @RequestMapping("/dynamic/praise")
    public ModelAndView dynamicPraise(HttpServletRequest request, @PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        ServerResponse serverResponse = dynamicAndPraiseVOService.getByCustomerId(id);
        if (!MessageUtil.STATUS_FAIL.equals(serverResponse.getStatus()))
            mav.addObject("dynamicAndPraiseVOList", serverResponse.getData());
        mav.setViewName("customer/personCenter");
        return this.setCommonPart(mav, id, "dynamic", 3);
    }

    @RequestMapping("/dynamic/collect")
    public ModelAndView dynamicCollect(HttpServletRequest request, @PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        ServerResponse serverResponse = dynamicAndCollectVOService.getByCustomerId(id);
        if (!MessageUtil.STATUS_FAIL.equals(serverResponse.getStatus()))
            mav.addObject("dynamicAndCollectVOList", serverResponse.getData());
        mav.setViewName("customer/personCenter");
        return this.setCommonPart(mav, id, "dynamic", 4);
    }

    /* ==================================== 刷题模块 ==================================== */
    @RequestMapping("/choice")
    public ModelAndView choice(HttpServletRequest request, @PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        ServerResponse serverResponse = questionSetService.getByCustomerId(id);
        if (!MessageUtil.STATUS_FAIL.equals(serverResponse.getStatus()))
            mav.addObject("list", serverResponse.getData());
        mav.setViewName("customer/personCenter");
        return this.setCommonPart(mav, id, "choice", 1);
    }

    @RequestMapping("/choice/comment")
    public ModelAndView choiceComment(HttpServletRequest request, @PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("customer/personCenter");
        return this.setCommonPart(mav, id, "choice", 2);
    }

    @RequestMapping("/choice/collect")
    public ModelAndView choiceCollect(HttpServletRequest request, @PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        ServerResponse serverResponse = choiceAndCollectVOService.getByCustomerId(id);
        if (!MessageUtil.STATUS_FAIL.equals(serverResponse.getStatus()))
            mav.addObject("choiceAndCollectVOList", serverResponse.getData());
        mav.setViewName("customer/personCenter");
        return this.setCommonPart(mav, id, "choice", 3);
    }

    /* ================================== 在线编程模块 ================================== */
    @RequestMapping("/program")
    public ModelAndView program(HttpServletRequest request, @PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        ServerResponse serverResponse = programRecordVOService.getByCustomerId(id);
        if (!MessageUtil.STATUS_FAIL.equals(serverResponse.getStatus()))
            mav.addObject("programRecordVOList", serverResponse.getData());
        mav.setViewName("customer/personCenter");
        return this.setCommonPart(mav, id, "program", 1);
    }

    @RequestMapping("/program/comment")
    public ModelAndView programComment(HttpServletRequest request, @PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("customer/personCenter");
        return this.setCommonPart(mav, id, "program", 2);
    }

    @RequestMapping("/program/collect")
    public ModelAndView programCollect(HttpServletRequest request, @PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        ServerResponse serverResponse = programAndCollectVOService.getByCustomerId(id);
        if (!MessageUtil.STATUS_FAIL.equals(serverResponse.getStatus()))
            mav.addObject("programAndCollectVOList", serverResponse.getData());
        mav.setViewName("customer/personCenter");
        return this.setCommonPart(mav, id, "program", 3);
    }

    /* ==================================== 竞赛模块 ==================================== */
    @RequestMapping("/contest")
    public ModelAndView contest(HttpServletRequest request, @PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        ServerResponse serverResponse = contestAndMessageVOService.getByCustomerId(id);
        if (!MessageUtil.STATUS_FAIL.equals(serverResponse.getStatus()))
            mav.addObject("contestAndMessageVOList", serverResponse.getData());
        mav.setViewName("customer/personCenter");
        return this.setCommonPart(mav, id, "contest", 1);
    }

    /* ==================================== 消息模块 ==================================== */
    @RequestMapping("/message")
    public ModelAndView message(HttpServletRequest request, @PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        ServerResponse serverResponse = messageService.getAllByCustomerId(id);
        if (!MessageUtil.STATUS_FAIL.equals(serverResponse.getStatus()))
            mav.addObject("messageList", serverResponse.getData());
        mav.setViewName("customer/personCenter");
        return this.setCommonPart(mav, id, "message", 1);
    }

    @RequestMapping("/messageCheckAll")
    public ModelAndView messageCheckAll(HttpServletRequest request, @PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        Customer customer = (Customer) request.getSession().getAttribute("loginCustomer");
        messageService.readAllByCustomerId(customer.getCustomerId());
        mav.setViewName("redirect:/center/" + id + "/message");
        return mav;
    }

    /* ==================================== 公共方法 ==================================== */
    private ModelAndView setCommonPart(ModelAndView mav, Integer customerId, String type, Integer value) {
        mav.addObject("type", type);
        mav.addObject("value", value);
        mav.addObject("id", customerId);
        Customer customer = null;
        try {
            customer = (Customer) customerService.getById(customerId).getData();
            mav.addObject("loginCustomer", customer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }
}
